package com.czl.module_storehouse.adapter;

import android.annotation.SuppressLint;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.czl.base.data.bean.tengyun.LocatListBean;
import com.czl.base.data.bean.tengyun.ProductBean;
import com.czl.base.data.bean.tengyun.SortBean;
import com.czl.module_storehouse.R;
import com.czl.module_storehouse.bean.ShiftAddBean;
import com.czl.module_storehouse.listener.OnItemChangeListener;

import java.util.ArrayList;
import java.util.List;

/**
 * @author xubin
 */
public class ShiftAddAdapter extends BaseMultiItemQuickAdapter<ShiftAddBean, BaseViewHolder> {

    private OnItemChangeListener mOnItemChangeListener;

    /**
     * Same as QuickAdapter#QuickAdapter(Context,int) but with
     * some initialization data.
     *
     * @param data A new list is created out of this one to avoid mutable list
     */
    public ShiftAddAdapter(List<ShiftAddBean> data) {
        super(data);
        addItemType(ShiftAddBean.TYPE_PRODUCT, R.layout.item_shift_add_goods);
        addItemType(ShiftAddBean.TYPE_SORT, R.layout.item_shift_add);
    }

    public void addNewData(List<SortBean> sortBeans) {
        if (sortBeans == null) {
            return;
        }
        List<ShiftAddBean> list = new ArrayList<>();
        for (SortBean bean : sortBeans) {
            ShiftAddBean shiftAddBean = new ShiftAddBean();
            bean.setClickable(false);
            shiftAddBean.setSortBean(bean);
            list.add(shiftAddBean);
        }
        addData(list);
    }

    @SuppressLint("SetTextI18n")
    @Override
    protected void convert(@NonNull BaseViewHolder helper, ShiftAddBean item) {
        switch (helper.getItemViewType()) {
            case ShiftAddBean.TYPE_PRODUCT:
                ProductBean productBean = item.getProductBean();
                if (productBean == null) {
                    return;
                }
                helper.setText(R.id.tv_title, productBean.getProductName())
                        .setText(R.id.tv_code, "编号：" + productBean.getProductCode())
                        .setText(R.id.tv_loc, productBean.getStorehouseLocatName());
                helper.getView(R.id.iv_delete)
                        .setOnClickListener(v -> {
                            remove(item);
                            if (mOnItemChangeListener != null) {
                                mOnItemChangeListener.remove(helper.getLayoutPosition());
                            }
                        });
                break;
            case ShiftAddBean.TYPE_SORT:
                SortBean sortBean = item.getSortBean();
                if (sortBean == null) {
                    return;
                }
                helper.setText(R.id.tv_title, sortBean.getSortName())
                        .setText(R.id.tv_model, "编号：" + sortBean.getSortModel())
                        .setText(R.id.tv_loc, "位置：" + getLocTextByShiftNum(sortBean));
                TextView textView = helper.getView(R.id.tv_shift_num);
                textView.setText("移位：" + (sortBean.getProductList() == null ? 0 : sortBean.getProductList().size()));
                if (sortBean.pasteCode()) {
                    helper.getView(R.id.tv_loc).setVisibility(View.GONE);
                    helper.getView(R.id.iv_product_next).setVisibility(View.VISIBLE);
                    helper.getView(R.id.tv_shift_num).setVisibility(View.VISIBLE);
                } else {
                    helper.getView(R.id.tv_loc).setVisibility(View.VISIBLE);
                    helper.getView(R.id.iv_product_next).setVisibility(View.GONE);
                    helper.getView(R.id.tv_shift_num).setVisibility(View.GONE);
                }
                helper.getView(R.id.cl_root).setOnClickListener(v -> {
                    if (getOnItemChildClickListener() != null) {
                        getOnItemChildClickListener().onItemChildClick(ShiftAddAdapter.this, v, helper.getAbsoluteAdapterPosition());
                    }
                });
                helper.getView(R.id.tv_loc)
                        .setOnClickListener(v -> {
                            if (getOnItemChildClickListener() != null) {
                                getOnItemChildClickListener().onItemChildClick(ShiftAddAdapter.this, v, helper.getAbsoluteAdapterPosition());
                            }
                        });
                helper.getView(R.id.iv_delete)
                        .setOnClickListener(v -> {
                            remove(item);
                            if (mOnItemChangeListener != null) {
                                mOnItemChangeListener.remove(helper.getLayoutPosition());
                            }
                        });
                break;
            default:
                break;
        }
    }

    /**
     * getLocTextByShiftNum
     *
     * @param sortBean
     * @return
     */
    private String getLocTextByShiftNum(SortBean sortBean) {
        if (sortBean.getLocatList() == null || sortBean.getLocatList().isEmpty()) {
            return "--";
        }
        StringBuilder text = new StringBuilder();
        for (LocatListBean locatListBean : sortBean.getLocatList()) {
            String shiftNum = locatListBean.getShiftNum() == null ? "0" : String.valueOf(locatListBean.getShiftNum());
            text.append(locatListBean.getStorehouseLocatName())
                    .append("(")
                    .append(shiftNum)
                    .append(") ");
        }
        return text.toString();
    }

    public List<SortBean> getSortBeans() {
        List<SortBean> list = new ArrayList<>();
        for (ShiftAddBean bean : getData()) {
            SortBean sortBean = bean.getSortBean();
            if (sortBean != null) {
                list.add(sortBean);
            }
        }
        return list;
    }

    public List<ProductBean> getProducts() {
        List<ProductBean> list = new ArrayList<>();
        for (ShiftAddBean bean : getData()) {
            ProductBean productBean = bean.getProductBean();
            if (productBean != null) {
                list.add(productBean);
            }
        }
        return list;
    }

    public void setOnItemChangeListener(OnItemChangeListener onItemChangeListener) {
        mOnItemChangeListener = onItemChangeListener;
    }
}
